﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RMS.Model;
using RMS.View;

namespace RMS.Controller
{
    public class StudentResearchGroupController
    {
        private List<StudentResearchGroup> _model;
        private StudentResearchGroupView _view;

        public StudentResearchGroupController(StudentResearchGroupView view)
        {
            this._model = new List<StudentResearchGroup>();
            this._view = view;
        }
        public List<StudentResearchGroup> Model
        {
            get { return this._model; }
            set { this._model = value; }
        }
        public StudentResearchGroupView View
        {
            get { return this._view; }
            set { this._view = value; }
        }

        // TODO: Incomplete
        public void HandleAddGroupButtonClick()
        {
            if (this._view.TextBoxGroupName == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty group name");
#else
                MessageBox.Show("empty group name");
                return;
#endif
            }
            if (this._view.ComboBoxAcademicGroup == "")
            {
#if DEBUG
                throw new ArgumentNullException("empty academic program");
#else
                MessageBox.Show("empty academic program");
                return;
#endif
            }
            StudentResearchGroup group = new StudentResearchGroup(0, Convert.ToInt32(this._view.ComboBoxAcademicGroup), this._view.TextBoxGroupName, this._view.TextBoxDescription);
            this.AddStudentGroup(group);
        }

        private void AddStudentGroup(StudentResearchGroup group)
        {
            int id = Persistence.DataManager.Instance.AddStudentGroup(group);
            if (id > 0)
            {
                group.Id = id;
                this.Model.Add(group);
                this.View.PopulateGroupList(this.Model);
            }
            
        }

        public void HandleRemoveGroupButtonClick()
        {
            StudentResearchGroup group = new StudentResearchGroup(Convert.ToInt32(this._view.TextBoxGroupId), 0, "", "");
            this.RemoveStudentGroup(group);
        }

        private void RemoveStudentGroup(StudentResearchGroup group)
        {
            int retval = Persistence.DataManager.Instance.RemoveStudentGroup(group);
            if (retval == 0)
            {
                this.Model.Remove(group);
                this.View.PopulateGroupList(this.Model);
            }
        }

        public void HandleUpdateGroupButtonClick()
        {
            StudentResearchGroup group = new StudentResearchGroup(Convert.ToInt32(this._view.TextBoxGroupId), Convert.ToInt32(this._view.ComboBoxAcademicGroup), this._view.TextBoxGroupName, this._view.TextBoxDescription);
            this.UpdateStudentGroup(group);
        }

        private void UpdateStudentGroup(StudentResearchGroup group)
        {
            foreach (StudentResearchGroup currentGroup in this.Model)
            {
                if (currentGroup.Id == group.Id)
                {
                    currentGroup.Name = group.Name;
                    currentGroup.ProgramId = group.ProgramId;
                    currentGroup.Description = group.Name;
                    Persistence.DataManager.Instance.UpdateResearchGroup(group);
                    this.View.PopulateGroupList(this.Model);
                    break;
                }
            }
        }

        public void HandleSearcGroupClickButton()
        {
            throw new NotImplementedException();
        }

        internal void HandleCancelSearchGroupClickButton()
        {
            throw new NotImplementedException();
        }

        public void RefreshGroupStudentsList(int p)
        {
            foreach (StudentResearchGroup group in this._model)
            {
                if (group.Id == p)
                {
                    this._view.PopulateGroupStudentsList(group.Students);
                }
            }
        }
    }
}
